﻿using System.IO;
using ConsoleApplication2;
using Game1942.Library.View;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Input;

namespace Game1942.Library.Controller
{
    public class Algo42Controller
    {
        private Algo42 algo42;      
        private PlayerView view;
        private SoundEffect fireSound;


        public Algo42Controller(Algo42 player, PlayerView playerView)
        {
            algo42 = player;
            view = playerView;
        }

        public void SetFireSound(SoundEffect fireSound)
        {
            this.fireSound = fireSound;
        }

        public void KeyPressed(KeyboardState keyboardState, KeyboardState previousKeyboardState)
        {
            Keys[] pressedKeys = keyboardState.GetPressedKeys();
            view.SetNormalView();
            foreach (Keys key in pressedKeys)
            {
                switch (key)
                {
                    case Keys.Up: algo42.MoveUp(); break;
                    case Keys.Down: algo42.MoveDown(); break;
                    case Keys.Left:
                        {
                            algo42.MoveLeft();
                            view.SetLeftView();
                            break;
                        }

                    case Keys.Right:
                        {
                            algo42.MoveRight();
                            view.SetRightView();
                            break;
                        }
                }
                if (previousKeyboardState.IsKeyUp(key))
                {
                    switch (key)
                    {
                        case Keys.A: algo42.ShootLaser();
                            fireSound.Play(); break;
                        case Keys.S: algo42.ShootRocket();
                            fireSound.Play(); break;
                        case Keys.D: algo42.ShootTorpedo();
                            fireSound.Play(); break;
                        case Keys.Q:
                            {
                                var serialize = new Serialize<SaveUnit>();
                                serialize.serializeGame(new SaveUnit(), @Directory.GetCurrentDirectory() + "\\save.xml");
                                break;
                            }
                    }
                }
            }
        }
    }
}


